package com.after.saas.common.constants;

/**
 * 错误码表
 *
 * @author hjchen
 * @date 2021-4-27
 */
public enum ErrorCodeEnum implements IResultEnum {

    INNER_ERROE(500, "系统内部异常"),
    UNKNOWN(-1, "未知异常"),
    ERROR_400(400, "参数不合法"),
    AUTH_ERROR(403, "没有权限"),
    USER_NOT_EXIST(1001, "账号不存在"),
    USER_PASSWD_BAD(1002, "账号密码错误"),
    USER_DISABLED(1003, "账号被禁用"),
    USER_LOCKED(1004, "账号被被锁住"),
    ACCESS_TOKEN_EMPTY(1011, "TOKEN为空"),
    LOGOUT(1012, "已登出"),
    ACCESS_TOKEN_INVALID(1013, "TOKEN无效"),
    ACCESS_TOKEN_EXPIRE(1014, "TOKEN过期"),

    ;

    private Integer errorCode;
    private String errorMsg;

    ErrorCodeEnum(Integer errorCode, String errorMsg) {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public static String getError(Integer errorCode) {
        for (ErrorCodeEnum e : ErrorCodeEnum.values()) {
            if (errorCode.intValue() == e.getErrorCode().intValue()) {
                return e.getErrorMsg();
            }
        }
        return "未知业务异常";
    }

    /**
     * 通过枚举<code>code</code>获得枚举。
     *
     * @param code 错误码
     * @return
     */
    public static ErrorCodeEnum getEnumByCode(int code) {
        for (ErrorCodeEnum param : values()) {
            if (code == param.value()) {
                return param;
            }
        }
        return INNER_ERROE;
    }

    public Integer getErrorCode() {
        return errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    @Override
    public String message() {
        return errorMsg;
    }

    @Override
    public int value() {
        return errorCode;
    }

}
